/*package org.qs.webservice.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.qs.webservice.exception.QSReadException;
import org.qs.webservice.service.CertPayService;
import org.qs.webservice.util.IReadUtils;
import org.qs.webservice.util.StringUtils;
import org.qs.wx.handler.util.ConstantUtil;
import org.qs.wx.handler.util.TenpayUtil;
import org.springframework.stereotype.Service;

*//**
 * iread配置接口
 * @ClassName: ReadService 
 * @author xusaike 
 * @date 2015年9月30日 上午10:37:08 
 *
 *//*
@Service
public class CertPayServiceImpl implements CertPayService{
	
	@Resource
	UsPayMapper usPayMapper;
	@Resource
	UsBalanceMapper usBalanceMapper;
	@Resource
	UsProductMapper usProductMapper;
	@Resource
	IreadMemcacheService ireadMemcacheService;
	@Resource
	UpdateBalanceService updateBalanceService;
	@Override
	public String addOrder(String token,String productId,String spbill_create_ip ,HttpServletRequest request,
			HttpServletResponse response) {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(StringUtils.isNullOrEmpty(token)){
			Map<String,Object> error = new HashMap<String,Object>();
			error.put("login", "用户未登录");
			List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			list.add(error);
			tokenMap.put("code", 403);
			tokenMap.put("error",list);
			return IReadUtils.beanToJson(tokenMap); 
		}
		
		int productId_int=0;
		try {
			
			productId_int = Integer.valueOf(productId);
			
		} catch (NumberFormatException e) {
			Map<String,Object> error = new HashMap<String,Object>();
			error.put("param", "参数错误");
			List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			list.add(error);
			tokenMap.put("code", 403);
			tokenMap.put("error",list);
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("token", token);
			errorJson.put("productId", productId);
			QSReadException.printf(e, "数字转换异常！",errorJson);
			return IReadUtils.beanToJson(tokenMap); 
		}
		
		String userId = IReadUtils.getUserIdByToken(token, ireadMemcacheService);
		UsProduct usProduct = usProductMapper.getUsProductById(productId_int);
		if(StringUtils.isNullOrEmpty(userId)|| usProduct==null){
			Map<String,Object> error = new HashMap<String,Object>();
			error.put("token", "token失效");
			List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			list.add(error);
			tokenMap.put("code", 403);
			tokenMap.put("error",list);	
			return IReadUtils.beanToJson(tokenMap);
		}
		
		Date date = new Date();
		int dateInt = Integer.valueOf(DateUtil.getString(date, "yyyyMMdd"));
		UsPay pay = new UsPay();
		IdWorker idworker = new  IdWorker(10);
		Long id=idworker.nextId();
		pay.setPay_order_no(id);
		pay.setCreate_day(dateInt);
		pay.setCreate_time(date);
		pay.setModify_time(date);
		
		pay.setPay_org_id(2);
		pay.setUser_id(Long.valueOf(userId));
		pay.setOrder_status(1);
		pay.setOriginal_fee(usProduct.getProduct_price());
		pay.setProduct_id(usProduct.getProduct_id());
		pay.setCharge_fee(usProduct.getDiscount_price());
		try {
			usPayMapper.addOrder(pay);
			CertOrderInfo order = new CertOrderInfo();
			order.setAppid(ConstantUtil.APP_ID);
			order.setBody("艾美阅读");
			order.setDetail(usProduct.getProduct_name());
			order.setMch_id(ConstantUtil.PARTNER);
			order.setNonce_str(TenpayUtil.getRandomStringByLength(32).toUpperCase());
			order.setNotify_url("http://readapi.imread.com/api/cert/pay/result");
			order.setOut_trade_no(id+"");
			order.setSpbill_create_ip(spbill_create_ip);
			//order.setTotal_fee(1+"");
			order.setTotal_fee(usProduct.getDiscount_price()+"");
			order.setTrade_type("APP");
			
			//生成sign
			PrepayIdRequestHandler prepayReqHandler = new PrepayIdRequestHandler(request, response);//获取prepayid的请求类
			prepayReqHandler.setParameter("appid", ConstantUtil.APP_ID);
			prepayReqHandler.setParameter("body", "艾美阅读");
			prepayReqHandler.setParameter("detail", usProduct.getProduct_name());
			prepayReqHandler.setParameter("mch_id", ConstantUtil.PARTNER);
			prepayReqHandler.setParameter("nonce_str",order.getNonce_str());
			prepayReqHandler.setParameter("notify_url",order.getNotify_url());
			prepayReqHandler.setParameter("out_trade_no",id+"");
			prepayReqHandler.setParameter("spbill_create_ip", spbill_create_ip);
			prepayReqHandler.setParameter("total_fee", order.getTotal_fee());
			prepayReqHandler.setParameter("trade_type", order.getTrade_type());
			String sign = prepayReqHandler.createSHA1Sign();
			order.setSign(sign);
			tokenMap.put("code", 200);
			tokenMap.put("success",order);	
			return IReadUtils.beanToJson(tokenMap);
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("token", token);
			errorJson.put("productId", productId);
			QSReadException.printf(e, "生成订单异常！",errorJson);
			Map<String,Object> error = new HashMap<String,Object>();
			error.put("error", "生成订单失败");
			List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			list.add(error);
			tokenMap.put("code", 403);
			tokenMap.put("error",list);	
			return IReadUtils.beanToJson(tokenMap);
		}
		
	}
	@Override
	public String updateOrder(String orderno,  String status) {
		if(StringUtils.isNullOrEmpty(orderno)){
			return "403";
		}
		Long orderno_int;
		try {
			orderno_int = Long.valueOf(orderno);
		} catch (NumberFormatException e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("orderno", orderno);
			
			errorJson.put("status", status);
			
			QSReadException.printf(e, "类型转换异常！",errorJson);
			return "403";
		}
		UsPay pay = usPayMapper.getOrder(orderno_int);
		if(null==pay){
			CustomerLog.IMPayLog(logger,"订单号不存在:orderno:"+orderno+" status:"+status);
			//logger.error("订单号不存在:orderno:"+orderno+" status:"+status);
			return "403";
		}
		if(pay.getOrder_status()!=1){
			CustomerLog.IMPayLog(logger,"订单已处理过:orderno:"+orderno+" status:"+status);
			//logger.error("订单已处理过:orderno:"+orderno+" status:"+status);
			return "403";
		}
		
		pay.setOld_order_status(pay.getOrder_status());
		if("SUCCESS".equals(status))
			pay.setOrder_status(2);
		else
			pay.setOrder_status(3);
		
		
		String ret =  updateBalanceService.updateBalanceList("3", "1", pay.getOriginal_fee()+"", pay.getUser_id()+"", "", "", "", "", "", "", "微信充值", pay.getPay_order_no()+"", "", "", "", pay.getOrder_status()+"", "", 0);

		if(!"OK".equals(ret)){
			
			CustomerLog.IMPayLog(logger,"订单已处理过:orderno:"+orderno+" status:"+status);
			//logger.error("订单已处理过:orderno:"+orderno+" status:"+status);
			return "403";
		}

		
		return "200";
	}
	@Override
	public String updateIOSOrder(String orderno, String status) {
		if(StringUtils.isNullOrEmpty(orderno)){
			return "403";
		}
		Long orderno_int;
		try {
			orderno_int = Long.valueOf(orderno);
		} catch (NumberFormatException e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("orderno", orderno);
			errorJson.put("status", status);
			IMReadException.printf(e, "类型转换异常！",errorJson);
			return "403";
		}
		UsPay pay = usPayMapper.getOrder(orderno_int);
		if(null==pay){
			CustomerLog.IMPayLog(logger,"订单号不存在:orderno:"+orderno+" status:"+status);
			//logger.error("订单号不存在:orderno:"+orderno+" status:"+status);
			return "403";
		}
		
		
		if(pay.getOrder_status()!=1){
			CustomerLog.IMPayLog(logger,"订单已处理过:orderno:"+orderno+" orderStatus:"+pay.getOrder_status());
			//logger.error("订单已处理过:orderno:"+orderno+" orderStatus:"+pay.getOrder_status());
			return "403";
		}
		
		pay.setOld_order_status(pay.getOrder_status());
		if("success".equals(status))
			pay.setOrder_status(2);
		else
			pay.setOrder_status(3);
		
		
		
		String ret =  updateBalanceService.updateBalanceList("3", "1", pay.getOriginal_fee()+"", pay.getUser_id()+"", "", "", "", "", "", "", "iOS微信充值", pay.getPay_order_no()+"", "", "", "", pay.getOrder_status()+"", "", 0);
		
		if(!"OK".equals(ret)){
			CustomerLog.IMPayLog(logger,"订单已处理过:orderno:"+orderno+" orderStatus:"+pay.getOrder_status());
			//logger.error("订单已处理过:orderno:"+orderno+" orderStatus:"+pay.getOrder_status());
			return "403";
		}
		
		
		
		
		
		return "200";
	}

	
 
}*/